import { IpcMainEvent, IpcMainInvokeEvent } from 'electron';
import { WindowObject } from '@p/main-windowObject';

declare global {
	// 窗口对象列表
	var windowList: WindowObject[];
}

global.windowList = [];

/**
 * 获取窗口对象列表
 */
export function getWindowList() {
	return global.windowList;
}

/**
 * 根据 id 对象获取窗口对象
 */
export function getWindowById(id: number) {
	return global.windowList.find((item) => item.id === id);
}

/**
 * 根据 IpcMainEvent 对象获取窗口对象
 */
export function getWindowByEvent(event: IpcMainEvent | IpcMainInvokeEvent) {
	return getWindowById(event.sender.id) as WindowObject;
}

/**
 * 根据 id 从窗口对象列表删除窗口对象
 */
export function removeWindowById(id: number) {
	for (let i = 0; i < global.windowList.length; i++) {
		if (global.windowList[i].id === id) {
			global.windowList.splice(i, 1);
			break;
		}
	}
}
